<?php
/*
$SNAME = session_name("JANSENSESSION");
if (isset($_COOKIE[$SNAME]) || isset($_GET[$SNAME])) {
$SID = isset($_COOKIE[$SNAME]) ? $_COOKIE[$SNAME] : $_GET[$SNAME];
session_id($SID);
} else {
$SID = session_id();
}

session_start();
setcookie(session_name(), session_id(), time()+3600*24*365, "/");
*/
require_once './include/common.inc.php';
require_once './include/email.class.php';
checkLogin();

$page_title = 'jans2en furniture - Container - Confirmation';

/*
$cart = new cart();
$info_ary = $cart->getData();
$total_qty = $cart->getSortCount();
$total_cost = $cart->getTotalCost();
*/

$info_ary = $JANSEN_CONTAINER_ARY;
$total_qty = $JANSEN_CONTAINER_TOTAL_QTY;
$total_qty2 = $JANSEN_CONTAINER_TOTAL_QTY2;
$total_cost = $JANSEN_CONTAINER_TOTAL_COST;
$total_cost1 = $JANSEN_CONTAINER_TOTAL_COST1;
$total_cbm = $JANSEN_CONTAINER_TOTAL_CBM;
$total_cuft = $JANSEN_CONTAINER_TOTAL_CUFT;

//echo "<br/>cart_submit_cost:".$total_cost;

if (empty($info_ary)) {
    header("Location:cart_view.php");
    exit();
}

	//fabrics
$_fabric_item=array();
$sql = "SELECT * FROM `p_fabric` ORDER BY `seq`";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) 
{
   
   if(!empty($rs['name']))
   {       
    $_fabric_item[$rs['id']]=strtoupper($rs['name']);	
	}
}

//leather
$_leather_item=array();
$sql = "SELECT * FROM `p_leather` ORDER BY `seq`";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) 
{
   if(!empty($rs['name']))
    $_leather_item[$rs['id']]=strtoupper($rs['name']);
}


$all_product_info_ary = array();
$sql_product_info_ary = array();
$fitemid_ary = array();
foreach ($info_ary as $pid=>$vv) {
    @list($type, $pid,$fitemid,$fabid,$leaid) = explode("_", $pid);
    $pid_ary[] = $pid;
	$sql_product_info_ary[] = $type . '|' . $pid . '|' . $vv['code'] .  '|' . $vv['count'] . '|' . $vv['price'] . '|' . $vv['name']. '|' . $vv['fitemid']. '|' . $vv['fabid'] . '|' . $vv['leaid'];
	if(!empty($vv['fitemid']))
	{
		$fitemid_ary[] = $vv['fitemid'];
	}
}

$_finish_item =  array();
$fitemid_ary = array_unique($fitemid_ary);
if(!empty($fitemid_ary))
{
	$sql = "SELECT * FROM `p_finish_item` where id in(".implode(',',$fitemid_ary).")";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) 
	{
	   if(!empty($rs['name']))
	   {       
			$_finish_item[$rs['id']]=strtoupper($rs['name']);	
	   }
	}
}

$from="EN";
if (!empty($sql_product_info_ary)) {
    $sql_product_info_ary = implode("{}", $sql_product_info_ary);
    $sql_product_info_ary = addslashes($sql_product_info_ary);
	$sql = "INSERT INTO `order` (`product_info`, `totalcount`, `totalcost`, `user_id`, `date`,`from`) VALUES ('$sql_product_info_ary', '$total_qty', '$total_cost', '$jansen_uid', '$timestamp','$from')";
    $db->query($sql);
    $orderid = $db->insert_id();
}

// ��չ��ﳵ
$cart->emptyAll();
saveCart($db, $jansen_uid, "");

// ��ʱ�û�=����ʽ�û�
if ($jansen_status == 1) {
    $sql = "UPDATE `user` SET `status` = '2' WHERE `id`='" . $jansen_uid . "' AND `status` = 1";
    $db->query($sql);
}

if (!empty($pid_ary)) {
    $pid_str = implode(",", $pid_ary);
    $sql = "SELECT `id`, `code`,`pcode`, `small_pic` FROM `product` WHERE `id` IN (" . $pid_str . ")";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $all_product_info_ary[$rs['id']] = $rs;
    }
}

if ($jansen_type == 1) {
    $money_type = " HKD";
} else {
    $money_type = " USD";
}

$sql = "SELECT * FROM `user` WHERE `id`='$jansen_uid'";
$user_info_ary = $db->get_one($sql);


$sql = "SELECT * FROM `inventory`";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $location_info_ary[$rs['id']] = $rs['name'];
}

//Send Email
$dealer_name = $user_info_ary['firstname'];
$dealer_email = $user_info_ary['email'];

$system_ary = getSystemValue("");
$webmaster_name = $system_ary['webmaster_nameen'];
$webmaster_email = $system_ary['webmaster_emailen'];

//require_once './mail/order_to_dealer.php';
//echo $content;exit;
//sendhtmlmail($from_name,$from_email,$to_name,$to_email,$subject,$content);

require_once './mail/order_to_webmaster.php';
//echo $content;exit;
//sendhtmlmail($from_name,$from_email,$to_name,$to_email,$subject,$content);

//echo $content;
//exit;

// ����excel�ĵ�
$filename = './zh/attachments/order_' . $jansen_uid . '.xls';

$data = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style>
<!--
@page
	{margin:1.0in .75in 1.0in .75in;
	mso-header-margin:.5in;
	mso-footer-margin:.5in;}
br
	{mso-data-placement:same-cell;}
.style0
	{mso-number-format:General;
	text-align:general;
	vertical-align:bottom;
	white-space:nowrap;
	mso-rotate:0;
	mso-background-source:auto;
	mso-pattern:auto;
	color:windowtext;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	mso-font-charset:136;
	mso-protection:locked visible;
	mso-style-id:0;}
td
	{mso-style-parent:style0;
	padding-top:1px;
	padding-right:1px;
	padding-left:1px;
	mso-ignore:padding;
	color:windowtext;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	mso-font-charset:136;
	text-align:general;
	vertical-align:bottom;
	mso-background-source:auto;
	mso-pattern:auto;
	mso-protection:locked visible;
	white-space:nowrap;
	mso-rotate:0;}
.xl24
	{mso-style-parent:style0;
	font-size:8.0pt;
	font-family:Arial, sans-serif;
	mso-font-charset:0;}
.xl25
	{mso-style-parent:style0;
	font-size:8.0pt;
	font-weight:700;
	font-family:Arial, sans-serif;
	mso-font-charset:0;}
.xl26
	{mso-style-parent:style0;
	font-weight:700;}
ruby
	{ruby-align:left;}
rt
	{color:windowtext;
	font-size:9.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	mso-font-charset:136;
	mso-char-type:none;
	display:none;}
-->
</style>
</head>

<body>
<table width="100%" border="1" cellspacing="0" cellpadding="5">
<tr>
<!--<td class=xl25>Item No.</td>-->
<td class=xl25>Article #</td>
<td class=xl25>Sections</td>
<td class=xl25>Finish</td>
<td class=xl25>Fabric / Leather</td>
<td class=xl25>Description</td>
<td class=xl25>Qty</td>
<td class=xl25>Price(' . $money_type . ')</td>
<td class=xl25>Total(' . $money_type . ')</td>
<td class=xl25>Order date</td>
</tr>';

//$data = "Item No.\tSections\tQty\tPrice($money_type)\tOrder date\n";


foreach ($info_ary as $k=>$v) {
//    @list($type, $k, $fitemid) = explode("_", $k);
	@list($type, $k, $fitemid, $fabid, $leaid) = explode("_", $k);
//  $pid_ary[] = $pid;
    $location = $v['intype'] == 0 ? 'Products' : $location_info_ary[$v['intype']];
	 $fabric_choice = ! empty($v['fabid']) ? $_fabric_item[$v['fabid']] : $_leather_item[$v['leaid']];
    $data .= "<tr>
					  <td class=xl24>" . $v['code']. "</td> 
					  <td class=xl24>" . $location . "</td> 
					  <td class=xl24>" . $_finish_item[$v['fitemid']] . "</td> 
					  <td class=xl24>" . $fabric_choice . "</td> 
					  <td class=xl24>" . $v['name'] . "</td> 
					  <td class=xl24>" . $v['count'] . "</td> 
					  <td class=xl24>" . $v['price'] . "</td> 
					  <td class=xl24>" . $v['cost'] . "</td> 
					  <td class=xl24>" . date('d/M/Y') . "</td></tr>";
}

$data .= "
</table>
</body>
</html>";

if(count($info_ary)>0)
{
writeover($filename,$data,"rb+",1,1,0);

$attachment = fread(fopen($filename, "r"), filesize($filename));

$mail = new mime_mail();
$mail->from = "$from_name <$from_email>";
$mail->headers = "To: $to_name";

if (strstr($to_email, ",")) {
    $tmp_ary = explode(",", $to_email);
    foreach ($tmp_ary as $v) 
	{   
	   smail2($from_name,$from_email,$to_name,$v,$subject,$content,$filename);
    }  
	//$to = implode(",", $tmp_ary2);	

} else{
    $to   = "$to_name <$to_email>";
	smail2($from_name,$from_email,$to_name,$to_email,$subject,$content,$filename);
}
@unlink($filename);
}

	
function smail2($from_name,$from_email,$to_name,$to_email,$subject,$content,$att)
{
	if ($to_email == '')
		return;
	define('POSTMARKAPP_API_KEY', '179b751f-f2fc-479e-82b1-754f7cbcfa08');

// Create a "Sender signature", then use the "From Email" here.
// POSTMARKAPP_MAIL_FROM_NAME is optional, and can be overridden
// with Mail_Postmark::fromName()
	define('POSTMARKAPP_MAIL_FROM_ADDRESS',"info@jansenfurniture.com.hk");
	define('POSTMARKAPP_MAIL_FROM_NAME', $from_name);
	Mail_Postmark::compose()
    ->addTo($to_email,$to_name)
    ->subject($subject)
	->addAttachment($att)
    ->messageHtml($content)
    ->send();
	
}
	

require_once PrintEot('header');
require_once PrintEot('cart_submit');
require_once PrintEot('footer');

?>